<template>
  <div class="table-container">
    <el-table v-loading="page.loading" :border="false" :data="page.list" :show-header="false">
      <el-table-column label="商品">
        <template #default="scope">
          <div class="order-no">
            <span>订单号：{{ scope.row.orderNo }}</span>
            <span> 支付状态：<common-enum-view :enum-value="scope.row.orderStatus" :enum-name="'OrderStatus'" /> </span>
            <span> 支付方式：<common-enum-view :enum-value="scope.row.payType" :enum-name="'PayType'" /> </span>
          </div>
          <div class="order-info">
            <common-goods :goods-type="scope.row.goodsType" :goods-info="scope.row" />
            <div v-if="scope.row.orderStatus === 1 || scope.row.orderStatus === 3">
              <el-button link type="danger" @click="handleCancel(scope.row)"> 取消订单 </el-button>
              <el-button link type="primary" @click="handlePay(scope.row)"> 继续支付 </el-button>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <common-pagination v-model:current-page="page.pageCurrent" v-model:page-size="page.pageSize" :total="page.totalCount" @pagination="handlePage" />
  </div>
</template>
<script setup>
  import useTable from '~/utils/table.js'
  import { userApi } from '~/api/user.js'
  import { orderApi } from '~/api/order'

  const props = defineProps({
    orderStatus: {
      type: Number,
      default: 0
    }
  })

  // 继续支付
  function handlePay(row) {
    useRouter().push({ name: 'goods-order', query: { orderNo: row.orderNo, goodsType: row.goodsType, goodsId: row.goodsId } })
  }

  // 取消订单
  function handleCancel(row) {
    ElMessageBox.confirm('是否取消订单？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      orderApi.cancelOrder({ orderNo: row.orderNo }).then((res) => {
        ElMessage.success(res)
        handlePage()
      })
    })
  }

  const orderStatus = ref(props.orderStatus)
  const { page, handlePage } = useTable(
    {
      page: orderApi.orderPage
    },
    {
      orderStatus: orderStatus.value === 0 ? undefined : orderStatus.value
    }
  )
</script>
<style lang="scss" scoped>
  .order-no {
    line-height: 25px;
    span {
      margin-right: 20px;
    }
  }
  .order-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px 0;
    .order-info-course {
      max-width: 400px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
</style>
